package com.tunis.gtug.client;

import java.util.ArrayList;
import java.util.List;


import com.extjs.gxt.ui.client.fx.FxConfig;
import com.extjs.gxt.ui.client.util.Rectangle;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * @author ogabtni Entry point classes define <code>onModuleLoad()</code>.
 */
public class Tunis_gtug implements EntryPoint {
	
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting
	 * service.
	 */
	public static  final GreetingServiceAsync greetingService = GWT
			.create(GreetingService.class);
	/**
	 * This is the entry point method.
	 */
	public static ContentGrid grid;
	Tasks task;
	public static RootPanel rootPanel = RootPanel.get();

	public void onModuleLoad() {
		//rootPanel.setSize("1024", "768");
		//rootPanel.setStyleName("center");
		RootPanel.get("nameFieldContainer").setVisible(false);
		final Button sendButton = new Button("Open Sample Application!");
		sendButton.setStyleName("blue");
		final PushButton b = new PushButton(new Image("images/revert2.png"));
	
		b.setPixelSize(32, 32);
		b.setVisible(false);
		
		
		RootPanel.get("moveit").add(b);
	
		final TextBox nameField = new TextBox();
		nameField
				.setStyleName("html,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockquote,th,td");
		nameField.setText("Please Enter your name");
		final Label errorLabel = new Label();

		// We can add style names to widgets
		sendButton.addStyleName("sendButton");
//		final ContentPanel contentPanel = new ContentPanel();
//		contentPanel.setSize(800, 500);

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element

		// RootPanel.get().add(contentPanel);

		RootPanel rootPanel = RootPanel.get("nameFieldContainer");
		rootPanel.setStyleName("html,body,div,dl,dt,dd,ul,ol,li,h1,h2,h3,h4,h5,h6,pre,form,fieldset,input,p,blockquote,th,td");
		rootPanel.add(nameField);
		RootPanel.get("sendButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		final ChartContent chart = new ChartContent();

		RootPanel.get("centeredposition3").add(chart);
		chart.hide();

		// Focus the cursor on the name field when the app loads
		nameField.setFocus(true);
		nameField.selectAll();

		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				b.setVisible(true);
				RootPanel.get("imagegtug").setVisible(false);
				showGrid();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					showGrid();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a
			 * response.
			 */
			private void showGrid() {
				List<Developper> models = new ArrayList<Developper>();
				models.add(new Developper(nameField.getText(), "", "", ""));
				sendButton.setVisible(false);
				nameField.setVisible(false);
				RootPanel.get("label1").setVisible(false);
				grid = new ContentGrid(models);
			
				grid.setHeight(450);
				task = new Tasks();
				task.setStyleName("window3");
				task.setHeight(450);
				task.setWidth(355);

				grid.setStyleName("window");
				// contentPanel.add(grid,new
				// BorderLayoutData(LayoutRegion.CENTER));
				// RootPanel.get("nameFieldContainer").add(chart);
				RootPanel.get("centeredposition1").add(task);
				RootPanel.get("centeredposition2").add(grid);
				chart.setStyleName("window2");
				chart.setHeight(450);
				chart.show();

			}
		}
		
		b.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
				
				Rectangle rect = chart.el().getBounds();  
		          chart.el().setXY(rect.x - 1000, rect.y + 0,new FxConfig(1500));
		          Rectangle rect2 = task.el().getBounds(); 
		          
		          task.el().setXY(rect.x + 90, rect.y + 0, new FxConfig(1500));
		          task.setWidth(320);
		          chart.setWidth(360);
		         chart.getContentPanel().setWidth(350);
		         b.setVisible(false);}
			
					
					
				
			});

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
		nameField.addKeyUpHandler(handler);
	
	
				 
			
		 
	
	}
}
